package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.annotation.AppSecurity;
import com.ruoyi.app.service.AppUserInfoService;
import com.ruoyi.app.util.ReqMsg;
import com.ruoyi.app.util.RspMsg;
import com.ruoyi.bizsys.domain.AppUser;
import com.ruoyi.bizsys.mapper.AppUserMapper;
import com.ruoyi.bizsys.utils.UserCacheService;
import com.ruoyi.business.service.IAppService;
import com.ruoyi.business.service.ILoanService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sms.domain.SmsSendReq;
import com.ruoyi.sms.domain.SmsSendVo;
import com.ruoyi.sms.service.ISmsService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/app/userinfo")
public class AppUserInfoController extends BaseController {

    @Autowired
    private AppUserInfoService appUserInfoService;

    @Autowired
    UserCacheService userCacheService;

    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private IAppService appService;

    @Autowired
    private ISmsService smsService;

    @Autowired
    private ILoanService loanService;

    @AppSecurity
    @ApiOperation("发送删除账号验证码")
    @Log(title = "检查用户借据状态，发送验证码", businessType = BusinessType.SMS)
    @PostMapping("/send")
    public RspMsg sendSms(@Validated @RequestBody ReqMsg reqMsg) {
        SmsSendReq smsReq = JSON.parseObject(reqMsg.getData().toString(), SmsSendReq.class);
        smsReq.setSmsType("delete_account");
        checkAttribute(smsReq);
        // appUserInfoService.sendSms(smsReq, getCustNo());

        String custNo = userCacheService.getUserCacheInfo().getCustNo();
        log.debug("custNo = {}", custNo);

        // 检查在途订单
        appUserInfoService.checkInTransitOrders(custNo);

        //检查借据
        loanService.checkIou(getCustNo());

        //发送验证码
        smsService.sendVerifyCode(smsReq, true);
        return RspMsg.ok();
    }

    @AppSecurity
    @ApiOperation("删除账号")
    @Log(title = "修改用户状态，实名信息状态，保存删除理由", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public RspMsg delete(@Validated @RequestBody ReqMsg reqMsg) {
        // 参数转换
        JSONObject data;
        if (null == reqMsg.getData() && StringUtils.isBlank(reqMsg.getData().toString())) {
            throw new BusinessException(RspEnmu.REASON_NOTNULL);
        }

        data = JSONObject.parseObject(reqMsg.getData().toString());
        String dictSort = data.get("dict_sort").toString();
        String msgcode = data.get("code").toString();
        String reason = data.get("reason").toString();
        if (StringUtils.isBlank(dictSort) || StringUtils.isBlank(msgcode)) {
            throw new BusinessException(RspEnmu.PARAM_CANNOT_NULL);
        }

        if (StringUtils.isNotBlank(reason) && reason.length() > 150) {
            throw new BusinessException(RspEnmu.REASON_TOOLONG);
        }

        // 根据userId获取手机号
        AppUser appUser = appUserMapper.selectPhoneByUserId(getUserId());
        if (null == appUser) {
            return RspMsg.RspMsgOk();
        }

        // 短信验证码校验
        SmsSendVo smsVo = new SmsSendVo(null, appUser.getPhone(), appUser.getCountryCode(), "delete_account", null, msgcode, null);
        smsService.verifySmsCode(smsVo);

        //修改状态，保存理由表
        appService.deleteAccount(getCustNo(), dictSort, appUser, reason);
        return RspMsg.RspMsgOk();
    }

    @AppSecurity
    @ApiOperation("删除理由")
    @Log(title = "获取注销理由", businessType = BusinessType.GET)
    @PostMapping("/getReason")
    public RspMsg getReason(@Validated @RequestBody ReqMsg reqMsg) {
        return RspMsg.ok(appUserInfoService.getReason());
    }
}